package user;

import book.Library;
import utils.PermissionException;
import constant.Constant;

public class ProxyUser {
    public User realUser;

    public Library library;

    public ProxyUser(User user){
        this.realUser = user;
        library = Library.getLibrary();
    }
    public int display() {
        return this.realUser.display();
    }
    //----------------管理员操作方法--------------------------------------
    private void checkRealUserWhetherAdminUser(String msg){
        if(!(this.realUser instanceof AdminUser)){
            throw new RuntimeException(msg);
        }
    }
    //添加书籍操作
    public void addBook() {
        System.out.println("代理类的add方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限添加书籍");
        }catch (PermissionException e){
            e.printStackTrace();
        }

        ((AdminUser)(this.realUser)).addBook();
    }
    //更新书籍操作
    public void updateBook() {
        System.out.println("代理类的updateBook方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限更新书籍");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).updateBook();
    }
    //移除图书
    public void removeBook() {
        System.out.println("代理类的removeBook方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限下架书籍");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).removeBook();

    }
    //查看图书的借阅次数
    public void borrowCount( ) {
        System.out.println("代理类的borrowCount方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有查看借阅次数");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).borrowCount();
    }
    //查看最受欢迎的前K本书
    public void generateBook() {
        System.out.println("代理类的generateBook方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有查看最受欢的前k本书");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).generateBook();

    }
    //查看库存状态
    public void checkInventoryStatus() {
        System.out.println("代理类的checkInventoryStatus方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有查看库存状态");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).checkInventoryStatus();


    }
    //按照类别 统计图书
    public void categorizeBooksByCategory() {
        System.out.println("代理类的categorizeBooksByCategory方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限按照类别统计书籍");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).categorizeBooksByCategory();


    }
    //按照作者 统计图书
    public void categorizeBooksByAuthor() {
        System.out.println("代理类的categorizeBooksByAuthor方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限按照作者统计书籍");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).categorizeBooksByAuthor();

    }
    //移除上架超过1年的书籍
    public void checkAndRemoveOldBooks() {
        System.out.println("代理类的checkAndRemoveOldBooks方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限移除超过1年的书");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)(this.realUser)).checkAndRemoveOldBooks();

    }




//--------------------------------普通相关⽅法-----------------------------

    private void checkRealUserWhetherNormaIUser(String msg){
        if(!(this.realUser instanceof NormalUser)){
            throw new RuntimeException(msg);
        }
    }
    //借阅图书
    public void borrowBook() {
        System.out.println("代理类的borrowBook方法执行了");
        try {
            checkRealUserWhetherNormaIUser("管理员不能借书，请换更换为普通用户");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((NormalUser)(this.realUser)).borrowBook();


    }
    //归还图书
    public void returnBook() {
        System.out.println("代理类的returnBook方法执行了");
        try {
            checkRealUserWhetherNormaIUser("管理员不能还书，请换更换为普通用户");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((NormalUser)(this.realUser)).returnBook();

    }
    //查看个⼈借阅情况
    public void viewBorrowHistory() {
        System.out.println("代理类的viewBorrowHistory方法执行了");
        try {
            checkRealUserWhetherNormaIUser("管理员不能查看个人借阅情况，请换更换为普通用户");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((NormalUser)(this.realUser)).viewBorrowBooks();
    }





    //----------- 操作菜单---------------
    public void handleOperation(int choice) {
        if(this.realUser instanceof AdminUser){
            switch (choice){
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBook();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CATEGORIZE_BOOK_BY_CATEGORY:
                    categorizeBooksByCategory();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                    break;
                case Constant.CATEGORIZE_BOOK_BY_AUTHOR:
                    categorizeBooksByAuthor();
                    break;
                default:
                    break;
            }
        }else {
            switch (choice){
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBook();
                    break;
                case Constant.EXIT:
                    library.exit();
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowHistory();
                    break;
                default:
                    System.out.println("⽆效的操作。");
            }

        }

    }
}
